/*
 * @Auto: wl
 * @Date: 2020-04-26
 * @Description: 时间日期相关操作模块
 * @LastEditors: wl
 * @LastEditTime: 2020-06-05 16:07:59
 */

/**
 * @method 时间格式化
 * @param {string} datetime 国际化日期格式
 * @return {string} 日期字符
 * @desc 将 2018-09-23T11:54:16.000+0000 格式化成 2018/09/23 11:54:16
 */
export function format(datetime) {
  return formatWithSeperator(datetime, '-', ':', '0', 1)
}

/**
 * @method 时间格式化
 * @param {string} datetime 国际化日期格式
 * @param {string} dateSeprator 日期分隔符  
 * @param {string} timeSeprator 时间分隔符
 * @param {string} patch 时间占位符
 * @param {string} typeTime 时间类型
 * @return {string} 日期字符
 * @desc  将 2018-09-23T11:54:16.000+0000 格式化成类似 2018-09-23 11:54:16
 */
export function formatWithSeperator(datetime, dateSeprator, timeSeprator, patch, typeTime) {
  if (datetime) {
    const dateMat = new Date(datetime)
    const year = dateMat.getFullYear()
    let month, day, timeFormat
    if (dateMat.getMonth() + 1 < 10) {
      month = patch + (dateMat.getMonth() + 1)
    } else {
      month = dateMat.getMonth() + 1
    }
    if (dateMat.getDate() < 10) {
      day = patch + dateMat.getDate()
    } else {
      day = dateMat.getDate()
    }
    if (typeTime === 1) {
      const hh = initNum(dateMat.getHours(), 2)
      const mm = initNum(dateMat.getMinutes(), 2)
      const ss = initNum(dateMat.getSeconds(), 2)
      timeFormat = year + dateSeprator + month + dateSeprator + day + ' ' + hh + timeSeprator + mm + timeSeprator + ss
    } else {
      timeFormat = year + dateSeprator + month + dateSeprator + day
    }
    return timeFormat
  }
  return ''
}

export function initNum(data, num) {
  let ns = ''
  data = data + ''
  if (data.length < num) {
    let nm = num - data.length
    for (let i = 0; i < nm; i++) {
      ns = ns + '0'
    }
    data = ns + data
  }
  return data
}

/**
 * @method 时间格式化
 * @param {dateTime} date 国际化日期格式
 * @param {string} fmt 日期格式
 * @return {string} 日期字符
 * @desc  将 2018-09-23 11:54:16 格式化成类似 2018-09-23
 */
export function formatDate(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
    }
  }
  return fmt
}

/**
 * @method 时间增加
 * @param {dateTime} date 国际化日期格式
 * @param {int} day 日期天数
 * @return {string} 日期字符
 * @desc 增加对应天数
 */
export function addDate(date, day) {
  date = new Date(date)
  date = date + day * 1000 * 60 * 60 * 24
  date = new Date(date)
  return formatDate(date, 'yyyy-MM-dd')
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length)
}

function dateDiff(sDate1, sDate2) {
  let arrDate, objDate1, objDate2, iDays
  arrDate = sDate1.split('-')
  objDate1 = new Date(arrDate[1] + '/' + arrDate[2] + '/' + arrDate[0])
  arrDate = sDate2.split('-')
  objDate2 = new Date(arrDate[1] + '/' + arrDate[2] + '/' + arrDate[0])
  iDays = parseInt(Math.abs(objDate1 - objDate2) / 1000 / 60 / 60 / 24) // 相差毫秒数转成天数
  return iDays
}

/* today 是今天的时间戳(new Date().getTime()) */
function minStartDate(today, day) {
  let dayDiff = day * 24 * 60 * 60 * 1000 // day（多少天）的时间戳
  let minStartTime = today - dayDiff
  return format(minStartTime)
}

function dayFormat(date) {
  switch (new Date(date).getDay()) {
    case 0:
      return '星期日'
    case 1:
      return '星期一'
    case 2:
      return '星期二'
    case 3:
      return '星期三'
    case 4:
      return '星期四'
    case 5:
      return '星期五'
    case 6:
      return '星期六'
  }
}

export function formatSeconds(value) {
  var theTime = parseInt(value); // 秒  
  var theTime1 = 0; // 分  
  var theTime2 = 0; // 小时  
  if (theTime > 60) {
    theTime1 = parseInt(theTime / 60);
    theTime = parseInt(theTime % 60);
    if (theTime1 > 60) {
      theTime2 = parseInt(theTime1 / 60);
      theTime1 = parseInt(theTime1 % 60);
    }
  }
  var result = "" + parseInt(theTime) + "秒";
  if (theTime1 > 0) {
    result = "" + parseInt(theTime1) + "分" + result;
  }
  if (theTime2 > 0) {
    result = "" + parseInt(theTime2) + "小时" + result;
  }
  return result;
}

export function formatSeconds2(a) {
  var hh = parseInt(a / 3600);
  if (hh < 10) hh = "0" + hh;
  var mm = parseInt((a - hh * 3600) / 60);
  if (mm < 10) mm = "0" + mm;
  var ss = parseInt((a - hh * 3600) % 60);
  if (ss < 10) ss = "0" + ss;
  var length = hh + ":" + mm + ":" + ss;
  if (a > 0) {
    return length;
  } else {
    return "";
  }
}


/**
 * @description: 获取格式化当天的日期
 * @param {type} yyyy-MM-dd 当前的日期
 * @return: [yyyy-MM-dd 00:00:00, yyyy-MM-dd 23:59:59]
 * @author: wangdan
 */
export function forMatDayTime (date) {
  // 获取格林时间
  let date1 = new Date(new Date(new Date().toLocaleDateString()).getTime());
  let date2 = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1);
  // 获取00:00:00
  let start  = (date1.getHours()<10?"0"+date1.getHours():date1.getHours()) + ":" + (date1.getMinutes()<10?"0"+date1.getMinutes():date1.getMinutes()) + ":" + (date1.getSeconds()<10?"0"+date1.getSeconds():date1.getSeconds());
  // 获取23:59:59
  let end = date2.getHours() + ':' + date2.getMinutes() + ':' + date2.getSeconds();
  return [`${date} ${start}`,`${date} ${end}`]
}
